package com.hqyj.repertory.controller;
import com.hqyj.product.model.ProductInfo;
import com.hqyj.purchase.model.PurchaseInfo;
import com.hqyj.repertory.model.Repertory;
import frame.repertory.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@RequestMapping("/repertoryController")
public class RepertoryController extends BaseController {
    @RequestMapping("/list.do")
    public String repertoryList(HttpServletRequest request, HttpServletResponse response, Model model){
        ArrayList<Repertory> repertory_list = repertoryService.queryAllRepertory();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }
    @RequestMapping("/add.do")
    public String addRepertory(HttpServletRequest request, HttpServletResponse response, Model model,Repertory repertory){
        repertoryService.addRepertory(repertory);
        ArrayList<Repertory> repertory_list = repertoryService.queryAllRepertory();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }
    @RequestMapping("/repertory.do")
    public String queryRepertoryById(HttpServletRequest request, HttpServletResponse response, Model model,String idStr){

        Repertory repertory = repertoryService.queryRepertoryById(Integer.parseInt(idStr));

        model.addAttribute("repertory",repertory);

        return "/view/repertory/repertory/updateRepertory";

    }
    @RequestMapping("/update.do")
    public String updateRepertory(HttpServletRequest request, HttpServletResponse response, Model model,Repertory repertory){
        repertoryService.updateRepertory(repertory);
        ArrayList<Repertory> repertory_list = repertoryService.queryAllRepertory();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }
    @RequestMapping("/delete.do")
    public String deleteRepertory(HttpServletRequest request, HttpServletResponse response, Model model,String idStr){
        repertoryService.deleteRepertory(Integer.parseInt(idStr));
        ArrayList<Repertory> repertory_list = repertoryService.queryAllRepertory();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }
    @RequestMapping("/repertoryCondition.do")
    public String queryRepertoryCondition(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        System.out.println("1");
        ArrayList<Repertory> repertory_list = repertoryService.queryRepertoryCondition();
        if(repertory_list.size()>0){
           model.addAttribute("message","以下物品储备不足，请按时加货");
           model.addAttribute("repertory_list",repertory_list);
        }else{
            model.addAttribute("message","仓储充足，试试其他操作吧");
            repertory_list = repertoryService.queryAllRepertory();
            model.addAttribute("repertory_list",repertory_list);
        }
        return "/view/repertory/repertory/repertoryList";
    }
    @RequestMapping("/addPurchase.do")
    public String addPurchase(HttpServletRequest request, HttpServletResponse response, Model model,String name,String num){
        System.out.println("进来了");
        PurchaseInfo purchaseInfo = new PurchaseInfo();
        purchaseInfo.setPurchaseNum(100-Integer.parseInt(num));
        purchaseInfo.setTradeName(name);
        ArrayList<PurchaseInfo> purchaseInfos_db = repertoryService.queryAllNotPurchase();
        for (PurchaseInfo purchaseInfo1 :purchaseInfos_db){
            if(purchaseInfo1.getTradeName().equals(name)){
                model.addAttribute("message","已通知，请勿重复操作");
                ArrayList<Repertory> repertory_list = repertoryService.queryRepertoryCondition();
                model.addAttribute("repertory_list",repertory_list);
                return "/view/repertory/repertory/repertoryList";
            }
        }
        repertoryService.addPurchaseInfo(purchaseInfo);
        model.addAttribute("message","通知成功，敬请稍后");
        ArrayList<Repertory> repertory_list = repertoryService.queryRepertoryCondition();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }

    @RequestMapping("/addProduct.do")
    public String addProduct(HttpServletRequest request, HttpServletResponse response, Model model,String name,String num){
        ProductInfo productInfo = new ProductInfo();
        Integer i = 100-Integer.parseInt(num);
        productInfo.setProductQuantity(i);
        productInfo.setProductName(name);

        ArrayList<ProductInfo> productInfos_db = repertoryService.queryAllNotProduct();
        for (ProductInfo productInfo1 :productInfos_db){
            if(productInfo1.getProductName().equals(name)){
                model.addAttribute("message","已通知，请勿重复操作");
                ArrayList<Repertory> repertory_list = repertoryService.queryRepertoryCondition();
                model.addAttribute("repertory_list",repertory_list);
                return "/view/repertory/repertory/repertoryList";
            }
        }
        repertoryService.addProductInfo(productInfo);
        model.addAttribute("message","通知成功，敬请稍后");
        ArrayList<Repertory> repertory_list = repertoryService.queryRepertoryCondition();
        model.addAttribute("repertory_list",repertory_list);
        return "/view/repertory/repertory/repertoryList";
    }
}
